//
//  UITableViewCell+Custom.m
//  UniversalTableView
//
//  Created by 冷悦 on 16/3/11.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import <objc/runtime.h>
#import "UITableViewCell+Custom.h"

static const void *ModelKey = &ModelKey;

@implementation UITableViewCell (Custom)

@dynamic model;
- (id<CustomCellModel>)model{
    return objc_getAssociatedObject(self, ModelKey);
}

+ (UITableViewCell *)tableView:(UITableView *)tableView cellWithModel:(id<CustomCellModel>) model{
    NSString *className = model.className;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:className];
    
    if (cell == nil) {
        Class class = NSClassFromString(className);
        cell = [[class alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:className];
    }
    objc_setAssociatedObject(cell, ModelKey, model, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    cell.model = model;
    UIView *selectBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    selectBackgroundView.backgroundColor = SelectBackgroundColor230;
    cell.selectedBackgroundView = selectBackgroundView;
    return cell;
}

@end
